package com.liugh.controller.zhongche;

import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.standard.StandardItem;
import com.liugh.entity.standard.StandardSystem;
import com.liugh.service.zhongche.IStandardSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**标准体系维护 .
 * Created by Fatao on 2018/10/29 .
 */
@RestController
@RequestMapping("/standard_system")
@Api(description = "标准体系维护")
public class StandardSystemController extends BaseRestController {


    @Autowired
    IStandardSystemService standardSystemService;


    /**
     * 标准体系添加节点 .
     * @return .
     */
    @ApiOperation("标准体系增加")
    @PostMapping("/addSys")
    public ResponseModel<StandardSystem> addStandardSys(@RequestBody StandardSystem standardSystem) throws BusinessException {

        StandardSystem sys = standardSystemService.addSystem(standardSystem);

        if (sys == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        }
        return fillSuccessData(sys);
    }


    /**
     * 删除节点 .
     * @param id .
     * @return .
     */
    @ApiOperation("删除标准体系节点")
    @DeleteMapping("/delete")
    public ResponseModel<String> delStandardSys(@RequestParam("id") int id) {

        if (null == standardSystemService.getStandardSystemById(id)) {
            return fillFailData("此节点不存在");
        } else {
            return standardSystemService.delStandardSystem(id) > 0 ? fillSuccessData(PublicResultConstant.SUCCEED) : fillFailData(PublicResultConstant.ERROR);
        }

    }

    /**
     * 更新节点 .
     * @param standardSystem .
     * @return .
     */

    @ApiOperation("更新节点")
    @PutMapping("/update")
    public ResponseModel<StandardSystem> updateStandardSys(@RequestBody StandardSystem standardSystem) throws BusinessException {

        if (null == standardSystemService.getStandardSystemById(standardSystem.getId())) {
            throw new BusinessException(PublicResultConstant.ERROR);
        } else {
            return fillSuccessData(standardSystem);
        }

    }


    /**
     * 获取所有节点 .
     *
     * @return .
     */
    @ApiOperation("获取所有的父节点")
    @GetMapping("/get_parent")
    public ResponseModel<List<StandardSystem>> getStandParentSys() {
        return fillSuccessData(standardSystemService.getStandardSystemList());
    }


    /**
     * 获取该节点下的标准 .
     * @param parentId .
     * @return .
     */
    @ApiOperation("获取节点下的标准")
    @GetMapping("/get_item")
    public ResponseModel<List<StandardSystem>> getStandardSystemItem(@RequestParam("parentId") int parentId) {
        return fillSuccessData(standardSystemService.getStandardSystemItem(parentId));
    }

    /**
     * 根据id获取节点 .
     * @param id .
     * @return .
     */
    @ApiOperation("根据id获取")
    @GetMapping("get")
    public ResponseModel<StandardSystem> getStandardSysById(@RequestParam("id") int id) {
        return fillSuccessData(standardSystemService.getStandardSystemById(id));
    }

    /**
     * 根据standNo来查询十条记录 .
     * @param standNo .
     * @return .
     */
    @ApiOperation("根据标准编号来查询十条记录")
    @GetMapping("/get_standard")
    public ResponseModel<List<StandardItem>> findItemByNo(@RequestParam(value = "standNo",required = true) String standNo) {
        return fillSuccessData(standardSystemService.findItemByNo(standNo));
    }








}
